<template>
  <div class="app-container">
    <el-col :span="1.5">
      <el-button
        type="success"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
      >新增</el-button>
    </el-col>
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch"
             label-width="68px" style="display: flex; justify-content: flex-end;">

      <el-form-item label="房间类型" prop="roomType">
        <el-select v-model="queryParams.roomType"
                   clearable
                   @keyup.enter.native="handleQuery" placeholder="请选择">
          <el-option
            v-for="roomStatusitem in roomTypeDict"
            :key="roomStatusitem.code"
            :label="roomStatusitem.name"
            :value="roomStatusitem.code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="房间状态" prop="roomStatus">
        <el-select v-model="queryParams.roomStatus"
                   clearable
                   @keyup.enter.native="handleQuery" placeholder="请选择">
          <el-option
            v-for="roomStatusitem in roomStatusList"
            :key="roomStatusitem.value"
            :label="roomStatusitem.label"
            :value="roomStatusitem.value">
          </el-option>
        </el-select>

<!--        <el-input-->
<!--          v-model="queryParams.roomStatus"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10">
      <el-divider content-position="left">房间信息</el-divider>
    </el-row>
    <el-table v-loading="loading" :data="roomInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand">
            <div v-for="item in props.row.elder">
              <el-button class="expand-button" type="primary" size="mini" plain>{{ item.bedCode }}</el-button>
              <span class="elder-info">床位住户：{{ item.elderName ? item.elderName : '无'  }}</span>
              <el-button class="el-button" size="mini" :type="item.bedStatus === '空闲' ? 'success' : 'warning'"
                         round>{{ item.bedStatus}}</el-button>
            </div>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="房间编码(号)" align="center" prop="roomCode" />
      <el-table-column label="公寓名称" align="center" prop="apartmentName" />
      <el-table-column label="房间类型" align="center" prop="roomType" >
        <template slot-scope="scope">
          <el-tag >{{scope.row.roomType}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="基本设施" align="center" prop="baseFacilities" show-overflow-tooltip/>
      <el-table-column label="房间描述" align="center" prop="describe" show-overflow-tooltip/>
      <el-table-column label="房间状态" align="center" prop="roomStatus" >
        <template slot-scope="scope">
          <el-tag type="success">{{scope.row.roomStatus}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="护理人员" align="center" prop="nurseName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公寓房间信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body center="true">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公寓名称" prop="apartmentId">
              <el-select v-model="form.apartmentId" placeholder="请选择">
                <el-option
                  v-for="item in apartmentList"
                  :key="item.apartmentId"
                  :label="item.apartmentName"
                  :value="item.apartmentId">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="房间编码" prop="roomCode">
              <el-input v-model="form.roomCode" placeholder="请输入房间编码" style="width: 200px"/>
            </el-form-item>
            <el-form-item label="房间类型" prop="roomType">
              <el-select v-model="form.roomType" placeholder="请选择">
                <el-option
                  v-for="item in roomTypeDict"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="性别" prop="roomSex">
              <el-select v-model="form.roomSex" placeholder="请选择" style="width: 100px">
                <el-option label="男" value="男"></el-option>
                <el-option label="女" value="女"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="房间状态" prop="roomStatus">
              <el-select v-model="form.roomStatus" placeholder="请选择">
                <el-option
                  v-for="item in roomStatusList"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="基本设施" prop="baseFacilities">
              <el-input v-model="form.baseFacilities" placeholder="请输入基本设施"
                        type="textarea"
                        maxlength="250"
                        show-word-limit/>
            </el-form-item>
            <el-form-item label="房间描述" prop="describe">
              <el-input v-model="form.roomDescribe" placeholder="请输入房间描述"
                        type="textarea"
                        maxlength="250"
                        show-word-limit/>
            </el-form-item>
            <el-form-item label="护理人员" prop="nurseId">
              <el-select v-model="form.nurseId" placeholder="请选择">
                <el-option
                  v-for="item in nurseMemberList"
                  :key="item.memberId"
                  :label="item.memberName"
                  :value="item.memberId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listRoomInfo,
    getRoomInfo,
    delRoomInfo,
    addRoomInfo,
    updateRoomInfo,
    getRoomAllInfo
  } from "@/api/business/roomInfo";
  import {getElderApartment} from "@/api/business/elderlyapartment";
  import {getApartmentDictBySuperCode, getApartmentDictList, listApartmentDict} from "@/api/business/apartmentDict";
  import {getBeds} from "@/api/business/beds";
  import {getNurseMember} from "@/api/business/memberInfo";

export default {
  name: "RoomInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公寓房间信息表格数据
      roomInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        apartmentId: null,
        roomId: null,
        roomCode: null,
        roomType: null,
        roomStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      apartmentList: {},
      dictObject:{},
      roomStatusDict:{},
      roomTypeDict:{},
      roomStatusList: [{
        value: '201',
        label: '停用'
      }, {
        value: '202',
        label: '满员'
      }, {
        value: '203',
        label: '未住满'
      }, {
        value: '204',
        label: '维护'
      }],
      nurseMemberList:{},
    };
  },
  created() {
    this.getList();
    this.getApartment();
    this.getApartDict();
    this.getNurseMember();
  },
  methods: {
    /** 查询公寓房间信息列表 */
    getList() {
      this.loading = true;
      // listRoomInfo
      getRoomAllInfo(this.queryParams).then(response => {
        this.roomInfoList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        apartmentId: null,
        roomId: null,
        roomCode: null,
        roomType: null,
        baseFacilities: null,
        describe: null,
        roomPrice: null,
        roomStatus: null,
        createUserId: null,
        createTime: null,
        updateUserId: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公寓房间信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // const id = row.id || this.ids
      const query = {id:row.Id};
      getRoomInfo(query).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公寓房间信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRoomInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRoomInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除公寓房间信息编号为"' + ids + '"的数据项？').then(function() {
        return delRoomInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/roomInfo/export', {
        ...this.queryParams
      }, `roomInfo_${new Date().getTime()}.xlsx`)
    },
    getApartment(){
      getElderApartment().then(response => {
        this.apartmentList = response.rows;
      });
    },
    getApartDict(){
      getApartmentDictList().then(response => {
        this.dictObject = response;
        // this.roomStatusDict = this.dictObject.roomStatus;
        this.roomTypeDict = this.dictObject.roomType;
      });
    },
    getNurseMember(){
      getNurseMember().then(response =>{
        this.nurseMemberList = response.data;
      })
    }
  }
};
</script>
<style>
  .demo-table-expand {
    text-align: center;
  }

  .expand-item {
    margin: 10px;
    text-align: center;
  }

  .expand-button {
    margin-right: 5px;
  }

  .elder-info {
    margin-right: 5px;
    font-weight: bold;
  }
</style>

